using System;
using System.ComponentModel;
using System.Transactions;

using SIS.ENT;
using SIS.DAL;
using System.Data.SqlClient;
using System.Configuration;


namespace SIS.BLL {

/// <summary>
/// The FamilyMemberProfileTableManager class is responsible for managing BO.FamilyMemberProfileTable objects in the system.
/// </summary>
[DataObjectAttribute()]
public class FamilyMemberProfileTableManager
  {

#region "Public Methods"

/// <summary>
/// Gets a list with all FamilyMemberProfileTable objects in the database.
/// </summary>
/// <returns>A list with all FamilyMemberProfileTable from the database when the database contains any, or null otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static FamilyMemberProfileTableList GetList(){
return FamilyMemberProfileTableDB.GetList();
}

/// <summary>
/// Gets a single FamilyMemberProfileTable from the database without its data.
/// </summary>
/// <param name="familyMemberProfileKey">The FamilyMemberProfileKey of the FamilyMemberProfileTable in the database.</param>
/// <returns>A FamilyMemberProfileTable object when the FamilyMemberProfileKey exists in the database, or <see langword="null"/> otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static FamilyMemberProfileTable GetItem(int familyMemberProfileKey){
return GetItem(familyMemberProfileKey, false);
}

/// <summary>
/// Gets a single TeacherProfileTable from the database without its data.
/// </summary>
/// <param name="teacherProfileKey">The TeacherProfileKey of the TeacherProfileTable in the database.</param>
/// <returns>A TeacherProfileTable object when the TeacherProfileKey exists in the database, or <see langword="null"/> otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static FamilyMemberProfileTable GetItem(int familyMemberProfileKey, int SchoolKey)
{
    return GetItem(familyMemberProfileKey, SchoolKey);
}


[DataObjectMethod(DataObjectMethodType.Select, false)]
public static FamilyMemberProfileTable GetItemByUserAccountKey(int UserAccountKey)
{
    FamilyMemberProfileTable myFamilyMemberProfileTable = FamilyMemberProfileTableDB.GetItemByUserAccountKey(UserAccountKey);
    return myFamilyMemberProfileTable;
    
}

/// <summary>
/// Gets a single FamilyMemberProfileTable from the database.
/// </summary>
/// <param name="familyMemberProfileKey">The FamilyMemberProfileKey of the FamilyMemberProfileTable in the database.</param>
/// <param name="getFamilyMemberProfileTableRecords">Determines whether to load all associated FamilyMemberProfileTable records as well.</param>
/// <returns>
/// A FamilyMemberProfileTable object when the FamilyMemberProfileKey exists in the database, or <see langword="null"/> otherwise.
/// </returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static FamilyMemberProfileTable GetItem(int familyMemberProfileKey, bool getFamilyMemberProfileTableRecords){
FamilyMemberProfileTable myFamilyMemberProfileTable = FamilyMemberProfileTableDB.GetItem(familyMemberProfileKey);
return myFamilyMemberProfileTable;
}

/// <summary>
/// Saves a FamilyMemberProfileTable in the database.
/// </summary>
/// <param name="myFamilyMemberProfileTable">The FamilyMemberProfileTable instance to save.</param>
/// <returns>The new FamilyMemberProfileKey if the FamilyMemberProfileTable is new in the database or the existing FamilyMemberProfileKey when an item was updated.</returns>
[DataObjectMethod(DataObjectMethodType.Update, true)]
public static int Save(FamilyMemberProfileTable myFamilyMemberProfileTable){
using (TransactionScope myTransactionScope = new TransactionScope()){
int familyMemberProfileTableFamilyMemberProfileKey = FamilyMemberProfileTableDB.Save(myFamilyMemberProfileTable);

//  Assign the FamilyMemberProfileTable its new (or existing FamilyMemberProfileKey).
myFamilyMemberProfileTable.FamilyMemberProfileKey = familyMemberProfileTableFamilyMemberProfileKey;

myTransactionScope.Complete();

return familyMemberProfileTableFamilyMemberProfileKey;
}
}

/// <summary>
/// Deletes a FamilyMemberProfileTable from the database.
/// </summary>
/// <param name="myFamilyMemberProfileTable">The FamilyMemberProfileTable instance to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public static bool Delete(int FamilyMemberProfileKey)
{
return FamilyMemberProfileTableDB.Delete(FamilyMemberProfileKey);
}

[DataObjectMethod(DataObjectMethodType.Delete, true)]
public static bool Delete(FamilyMemberProfileTable myFamilyMemberProfileTable)
{
    SqlTransaction myTransaction = null;
    bool lResult = true;
    int count = 0;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        try
        {
            myConnection.Open();
            myTransaction = myConnection.BeginTransaction();             
            TeacherQualtificationTableList objTeacherQualification = new TeacherQualtificationTableList();
            TeacherOtherSkillsTableList objTeacherOtherSkill = new TeacherOtherSkillsTableList();
            TeacherJobExperiencesTableList objTeacherJobExp = new TeacherJobExperiencesTableList();
            TeacherDesignationTableList objTeacherDesignation = new TeacherDesignationTableList();
            TeacherSalaryTableList objTeacherSalary = new TeacherSalaryTableList();

            int UserAccountKey = 0;
            if (myFamilyMemberProfileTable != null)
            {
                UserAccountKey = myFamilyMemberProfileTable.UserAccountKey;

            }
            //Delete Profile Record
            lResult = FamilyMemberProfileTableDB.Delete(myFamilyMemberProfileTable.FamilyMemberProfileKey);             

            //Delete Address
            FamilyMemberAddressTable _objFamilyMemberAddress = FamilyMemberAddressTableDB.GetItemByUserAccountKey(myFamilyMemberProfileTable.UserAccountKey);
            if (_objFamilyMemberAddress != null)
            {
                lResult = FamilyMemberAddressTableDB.Delete(_objFamilyMemberAddress.FamilyMemberAddressKey);
            }
            //Delete Contact
            FamilyMemberContactTable _objFamilyMemberContact = FamilyMemberContactTableDB.GetItemByUserAccountKey(myFamilyMemberProfileTable.UserAccountKey);
            if (_objFamilyMemberContact != null)
            {
                lResult = FamilyMemberContactTableDB.Delete(_objFamilyMemberContact.FamilyMemberContactKey);
            }
            //Delete Relation
           FamilyMemberRelationshipTable _objFamilyMemberRelation= FamilyMemberRelationshipTableDB.GetItemByFamilyMemberProfileKey(myFamilyMemberProfileTable.FamilyMemberProfileKey);
           if (_objFamilyMemberRelation != null)
           {
               lResult = FamilyMemberRelationshipTableDB.DeleteByFamilyMemberProfileKey(_objFamilyMemberRelation.FamilyMemberProfileKey);
           }
             
            myTransaction.Commit();

        }

        catch (Exception ex)
        {
            myTransaction.Rollback();
            throw ex;
        }
        finally
        {
            myConnection.Close();
        }
    }

    return lResult;

}

#endregion

}

}
